
function queryLamps() {
    $.ajax({
        url: "/lamp/list",
        type: "POST",
        success: function (val) {
            if (val.retCode != 0) {
                alert(val.comment);
            } else {
                let lamps = val.data.lamps;
                let boards = val.data.boards;
                if (lamps == null) {
                    return;
                }
                let sdata = new Array();
                for (var key in lamps) {
                    let lamp = lamps[key];
                    if(lamp != null && lamp.disable == false) {
                        if (boards != null) {
                            if(lamp.boardid > 0) {
                                let board = boards[lamp.boardid];
                                if(board != null) {
                                    lamp.boardname = board.name;
                                }
                            }
                        }
                        sdata.push(lamp);
                    }
                }
                showLapms(sdata);
            }
        }
    });
}

function setOnOff(id, key) {
    $.ajax({
        url: "/lamp/" + key,
        type: "POST",
        data: {
            "id": id
        },
        success:  function(val) {
            alert(val.comment);
        }
    });
}

function showLapms(dt) {
    $("#jsGrid").jsGrid({
        height: "70%",
        width: "100%",
        //filtering: true,
        //editing: true,
        sorting: true,
        autoload: true,
        pageSize: 15,
        pageButtonCount: 5,
        onItemUpdated: function(args) {
            let oitem = args.previousItem;
            let item = args.item;
            let data = {
                id: item.id,
            };
            let url = "";

            if (oitem.name != item.name) {
                data.name= item.name;
                url = "/lamp/edit";
            }
            if (oitem.disable != item.disable) {
                data.disable= item.disable;
                url = "/lamp/edit";
            }

            if (url.length > 0) {
                postData(url, data)
            }

            if (oitem.onoff != item.onoff) {
                data.onoff= item.onoff;
                if (item.onoff == true) {
                    postData("/lamp/on", data)
                } else {
                    postData("/lamp/off", data)
                }
            }
        },
        fields: [
            { name: "id", title: "ID", type: "text", width: 50 },
            { name: "name", title: "名称", type: "text", width: 150 },
            { name: "boardname", title: "传感器", type: "text", width: 150 },
            { name: "key", title: "Key", type: "text", width: 200 },
            { name: "onoff", title: "开/关", sorting: false, formatter: function (value, grid, rows, state) {
                    return "<div class='btn-group' data-toggle='buttons-radio'> " +
					            "<button class='btn btn-primary' onclick='setOnOff("+ rows.id +", 'on')'>On</button>" +
                                "<button class='btn btn-primary' onclick='setOnOff("+ rows.id +", 'off')'>Off</button>" +
                            "</div>";
                }
            }
        ],
        data: dt,
    });
}
